package com.ebt.m.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.data.bean.ProposalListItem;
import com.ebt.m.proposal_v2.utils.DataUtils;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.utils.NumberHelper;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;
import com.ebt.m.commons.buscomponent.listview.IView;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ProposalAllItemView extends IView implements View.OnClickListener {

    private Context mContext;
    private TextView tv_name;
    private TextView tv_time;
    private TextView tv_content;
    private TextView tv_send;
    private TextView tv_title;
    private TextView tv_gender;
    private ImageView iv_image;
    private Button btn_delete;
    private ProposalListItem item;
    private LinearLayout mContent;

    public ProposalAllItemView(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        try {
            item = (ProposalListItem) args[0];
            tv_name.setText(item.applicantName);
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            tv_time.setText(format.format(new Date(Long.valueOf(item.createTime))));
            if (item.tabPage > 0) {
                tv_send.setVisibility(View.GONE);
            } else {
                tv_send.setVisibility(View.VISIBLE);
                tv_send.setText(getStatusName(item.status));
                tv_send.setTextColor(getStatusColor(item.status));
            }
            setDeleteButton();
            tv_title.setText(item.mainRiskName);

            String coverageTotal = DataUtils.parseIntValue(item.totalCoverage);

            //处理脏数据，有的带"元"，有的不带"元"
            if (item.totalPremium.contains("元")) {
                tv_content.setText("主险保额：" + coverageTotal + " 总保费：" + item.totalPremium);
            } else {
                tv_content.setText("主险保额：" + coverageTotal + " 总保费：" + NumberHelper.formatDouble(Double.valueOf(item.totalPremium)));
            }
              tv_gender.setText(getSexName(item.sex) + " | " + item.age + "岁");
            ImageLoaderUtil.getInstance().loadImage(getContext(), new ImageLoader.Builder().url(item.thumbnail).imgView(iv_image).build());
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    private String getSexName(int sex) {
        String sexName = "男";
        if (sex == 0) {
            sexName = "女";
        } else if (sex == 1) {
            sexName = "男";
        }
        return sexName;
    }

    private void setDeleteButton() {
        int status = item.status;
        switch (status) {
            case 1:
            case 2:
                btn_delete.setVisibility(View.VISIBLE);
                break;
            default:
                btn_delete.setVisibility(View.GONE);
                break;
        }
    }

    public ProposalAllItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProposalAllItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        View contentView = inflate(context, R.layout.view_proposal_all_item, this);
        tv_name = (TextView) contentView.findViewById(R.id.tv_name);
        tv_time = (TextView) contentView.findViewById(R.id.tv_time);
        tv_content = (TextView) contentView.findViewById(R.id.tv_content);
        tv_send = (TextView) contentView.findViewById(R.id.tv_send);
        tv_title = (TextView) contentView.findViewById(R.id.tv_title);
        iv_image = (ImageView) contentView.findViewById(R.id.iv_image);
        tv_gender = (TextView) contentView.findViewById(R.id.tv_gender);
        btn_delete = (Button) contentView.findViewById(R.id.btn_delete);
        mContent = (LinearLayout) contentView.findViewById(R.id.content);
        mContent.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
    }

    private int getStatusColor(int status) {
        String colorName = "#333333";
        switch (status) {
            case 3:
                colorName = "#37ac68";
                break;
            case 2:
            case 1:
                colorName = "#ff7324";
                break;
            case 4:
                colorName = "#333333";
                break;
        }
        return Color.parseColor(colorName);

    }

    private String getStatusName(int status) {
        String statusName = "";
        switch (status) {
            case 1:
                statusName = "草稿";
                break;
            case 2:
                statusName = "未发送";
                break;
            case 3:
                statusName = "已发送";
                break;
            case 4:
                statusName = "已查看";
                break;
        }
        return statusName;
    }

    @Override
    public void onClick(View v) {
        if (mOnclickListner != null) {
            mOnclickListner.onClick(v, item);
        }
    }
}